package edu.psu.bd.math.grooms;

import java.awt.Dimension;
import java.awt.Font;
import java.util.Enumeration;
import javax.swing.JFrame;
import javax.swing.JTree;
import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeExpansionListener;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.ExpandVetoException;
import javax.swing.tree.TreePath;
import javax.swing.tree.TreeSelectionModel;
import org.cgsuite.CanonicalGame;
import org.cgsuite.CooledGame;
import org.cgsuite.plugin.GridEditorPanel;
import org.cgsuite.util.Rational;

public final class GoTree extends JTree
        implements TreeSelectionListener
, TreeExpansionListener{//, TreeWillExpandListener {
    
    private final GridEditorPanel gep;
    
    public GoTree(GoRoom r, GridEditorPanel gep) {
        
        super(r);
        this.gep = gep;
        
        this.getSelectionModel().setSelectionMode
                (TreeSelectionModel.SINGLE_TREE_SELECTION);
        
        this.addTreeSelectionListener(this);
        

        this.addTreeExpansionListener(this);
        
        this.setShowsRootHandles(true);
        
        this.setCellRenderer(new GoTreeCellRenderer());
        
        this.setFont(new Font(Font.MONOSPACED, Font.PLAIN, 12));
        
        /*for (Enumeration e = r.depthFirstEnumeration();
        e.hasMoreElements();) {
            this.makeVisible(
                    new TreePath(
                    ((GoRoom)e.nextElement()).getPath()));
        }*/
        
        
    }
    
    /** Required by TreeSelectionListener interface. */
    public void valueChanged(TreeSelectionEvent e) {
        GoRoom node = (GoRoom)
        this.getLastSelectedPathComponent();
        
        if (node == null) return;
        
        gep.setGrid(node.getGrid());
        
    }

    public void treeExpanded(TreeExpansionEvent event) {
        ((JFrame)this.getTopLevelAncestor()).pack();
    }

    public void treeCollapsed(TreeExpansionEvent event) {
    }
    
    
}